package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.CustomerQueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequiresPermissions(value = {"customer:potentialList", "潜在客户页面"}, logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        qo.setStatus(Customer.STATUS_COMMON);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectJobBySn("job");
        model.addAttribute("jobs", jobs);
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectJobBySn("source");
        model.addAttribute("sources", sources);
        List<SystemDictionaryItem> communicationMethods = systemDictionaryItemService.selectJobBySn("communicationMethod");
        model.addAttribute("ccts", communicationMethods);
        model.addAttribute("result", pageInfo);


        //查询
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market-Manager");
        model.addAttribute("sellers", sellers);
        return "/customer/potentialList";
    }

    @RequiresPermissions(value = {"customer:poolList", "客户池页面"}, logical = Logical.OR)
    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        qo.setStatus(Customer.STATUS_POOL);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        List<SystemDictionaryItem> communicationMethods = systemDictionaryItemService.selectJobBySn("communicationMethod");
        model.addAttribute("ccts", communicationMethods);
        model.addAttribute("result", pageInfo);


        //查询
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market-Manager");
        model.addAttribute("sellers", sellers);
        return "/customer/poolList";
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate", "客户新增/编辑"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer) {
        if (customer.getId() != null) {
            customerService.update(customer);
        } else {
            customerService.save(customer);
        }
        return new JsonResult(true, "操作成功");
    }

    @RequestMapping("/success")
    public void success(){
        System.out.println("成功");
    }

    @RequestMapping("/updateStatus")
    @RequiresPermissions(value = {"customer:updateStatus", "修改状态"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult updateStatus(Customer customer) {
        //customerService.updateStatus(customer.getStatus(),customer.getId());
        customerService.updateStatus(customer.getStatus(), customer.getId());
        return new JsonResult(true, "操作成功");
    }

    @RequestMapping("/fail")
    public void fail(String username){
        System.out.println("失败");
    }

    @RequestMapping("/fail2")
    public void fail2(){
        System.out.println("失败.sou");
    }

}
